{% extends "base.html" %}

{% block title %}病原体数据 - 滩羊疫病监测系统{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h1 class="h3">病原体数据</h1>
        <a href="{{ url_for('pathogen.create') }}" class="btn btn-primary">
            <i class="bi bi-plus-lg"></i> 新增病原体
        </a>
    </div>

    <!-- 搜索表单 -->
    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" class="row g-3">
                <div class="col-md-3">
                    {{ form.keyword.label(class="form-label") }}
                    {{ form.keyword(class="form-control", value=request.args.get('keyword', '')) }}
                </div>
                <div class="col-md-2">
                    {{ form.type.label(class="form-label") }}
                    {{ form.type(class="form-select") }}
                </div>
                <div class="col-md-2">
                    {{ form.date_from.label(class="form-label") }}
                    {{ form.date_from(class="form-control", type="date", value=request.args.get('date_from', '')) }}
                </div>
                <div class="col-md-2">
                    {{ form.date_to.label(class="form-label") }}
                    {{ form.date_to(class="form-control", type="date", value=request.args.get('date_to', '')) }}
                </div>
                <div class="col-md-3 d-flex align-items-end">
                    <div class="d-grid gap-2 d-md-flex w-100">
                        {{ form.submit(class="btn btn-primary flex-grow-1") }}
                        <a href="{{ url_for('pathogen.list') }}" class="btn btn-secondary flex-grow-1">重置</a>
                    </div>
                </div>
            </form>
        </div>
    </div>

    <div class="card">
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-striped">
                    <thead>
                        <tr>
                            <th>ID</th>
                            <th>
                                <a href="{{ url_for('pathogen.list', sort='name', order='asc' if sort == 'name' and order == 'desc' else 'desc', keyword=request.args.get('keyword', ''), type=request.args.get('type', ''), date_from=request.args.get('date_from', ''), date_to=request.args.get('date_to', '')) }}" class="text-dark text-decoration-none">
                                    名称
                                    {% if sort == 'name' %}
                                        <i class="bi bi-arrow-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>
                                <a href="{{ url_for('pathogen.list', sort='type', order='asc' if sort == 'type' and order == 'desc' else 'desc') }}" class="text-dark text-decoration-none">
                                    类型
                                    {% if sort == 'type' %}
                                        <i class="bi bi-arrow-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>
                                <a href="{{ url_for('pathogen.list', sort='isolation_date', order='asc' if sort == 'isolation_date' and order == 'desc' else 'desc') }}" class="text-dark text-decoration-none">
                                    分离时间
                                    {% if sort == 'isolation_date' %}
                                        <i class="bi bi-arrow-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>分离地点</th>
                            <th>
                                <a href="{{ url_for('pathogen.list', sort='created_at', order='asc' if sort == 'created_at' and order == 'desc' else 'desc') }}" class="text-dark text-decoration-none">
                                    创建时间
                                    {% if sort == 'created_at' %}
                                        <i class="bi bi-arrow-{{ 'down' if order == 'desc' else 'up' }}"></i>
                                    {% endif %}
                                </a>
                            </th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for pathogen in pathogens.items %}
                        <tr>
                            <td>{{ pathogen.id }}</td>
                            <td>{{ pathogen.name }}</td>
                            <td>{{ pathogen.type }}</td>
                            <td>{{ pathogen.isolation_date.strftime('%Y-%m-%d') }}</td>
                            <td>{{ pathogen.isolation_location }}</td>
                            <td>{{ pathogen.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                            <td>
                                <a href="{{ url_for('pathogen.edit', id=pathogen.id) }}" 
                                   class="btn btn-sm btn-outline-primary">
                                    <i class="bi bi-pencil"></i>
                                </a>
                                <button type="button" class="btn btn-sm btn-outline-danger"
                                        onclick="deletePath({{ pathogen.id }})">
                                    <i class="bi bi-trash"></i>
                                </button>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>

            <!-- 分页 -->
            {% if pathogens.pages > 1 %}
            <nav aria-label="Page navigation" class="mt-4">
                <ul class="pagination justify-content-center">
                    {% for page in pathogens.iter_pages() %}
                        {% if page %}
                            <li class="page-item {{ 'active' if page == pathogens.page else '' }}">
                                <a class="page-link" href="{{ url_for('pathogen.list', page=page, sort=sort, order=order, keyword=request.args.get('keyword', ''), type=request.args.get('type', ''), date_from=request.args.get('date_from', ''), date_to=request.args.get('date_to', '')) }}">
                                    {{ page }}
                                </a>
                            </li>
                        {% else %}
                            <li class="page-item disabled"><span class="page-link">...</span></li>
                        {% endif %}
                    {% endfor %}
                </ul>
            </nav>
            {% endif %}
        </div>
    </div>
</div>

<!-- 删除确认对话框 -->
<div class="modal fade" id="deleteModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">确认删除</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                确定要删除这条病原体数据吗？此操作不可恢复。
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                <form id="deleteForm" method="POST" style="display: inline;">
                    <button type="submit" class="btn btn-danger">确认删除</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
function deletePath(id) {
    const modal = new bootstrap.Modal(document.getElementById('deleteModal'));
    const form = document.getElementById('deleteForm');
    form.action = "{{ url_for('pathogen.delete', id=0) }}".replace('0', id);
    modal.show();
}
</script>
{% endblock %} 